﻿namespace TestTool.GUI.Controls.Device
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Controls;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Utils;
    using TestTool.GUI.Views;
    using TestTool.Proxies.Onvif;
    using TestTool.Tests.Common.Media;
    using TestTool.Tests.Common.TestEngine;

    internal class PtzPage : Page, IPtzView, IView
    {
        private PtzController _controller;
        private VideoContainer _videoWindow;
        private Button btnAddPTZConfig;
        private Button btnGetProfiles;
        private Button btnGetPtzUrl;
        private Button btnStartAll;
        private Button btnStartZoom;
        private Button btnStopAll;
        private Button btnStopZoom;
        private Button btnVideo;
        private Button btnVxStartMove;
        private Button btnVxStopMove;
        private Button btnXFromMinToMax;
        private Button btnXMax;
        private Button btnXMin;
        private Button btnYFromMinToMax;
        private Button btnYMax;
        private Button btnYMin;
        private Button btnZoomFromMinToMax;
        private Button btnZoomMax;
        private Button btnZoomMin;
        private CheckBox chkUseTimeout;
        private ComboBox cmbPTZProfiles;
        private IContainer components;
        private GroupBox gbProfile;
        private Panel gbUrls;
        private GroupBox groupBox1;
        private Label label1;
        private Label label2;
        private Label lblPtzUrl;
        private Label lblVx;
        private Label lblVy;
        private Label lblVZoom;
        private Label lblX;
        private Label lblY;
        private Label lblZoom;
        private NumericUpDown nudVx;
        private NumericUpDown nudVy;
        private NumericUpDown nudVzoom;
        private NumericUpDown nudX;
        private NumericUpDown nudY;
        private NumericUpDown nudZoom;
        private Panel panelAbsoluteMove;
        private Panel panelContiniusMove;
        private RadioButton rbAbsoluteMove;
        private RadioButton rbContuniousMove;
        private RadioButton rbRelativeMove;
        private TextBox tbMediaUrl;
        private TextBox tbPtzUrl;
        private TextBox tbReport;
        private TextBox tbTimeout;

        public PtzPage()
        {
            this.InitializeComponent();
            this._controller = new PtzController(this);
        }

        private void AbsoluteRelativeMove()
        {
            TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
            if (profile == null)
            {
                base.ShowPrompt("Select profile with PTZ configuration", "Error");
            }
            else
            {
                try
                {
                    this.Controller.AbosuteRelativeMove(this.rbAbsoluteMove.Checked, profile.token, this.nudX.Value, this.nudY.Value, this.nudZoom.Value);
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        private void btnAddPTZConfig_Click(object sender, EventArgs e)
        {
            try
            {
                TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
                this.Controller.AddPTZConfiguration(profile);
            }
            catch (Exception exception)
            {
                this.ShowError(exception);
            }
        }

        private void btnGetProfiles_Click(object sender, EventArgs e)
        {
            try
            {
                this.Controller.GetProfiles();
            }
            catch (Exception exception)
            {
                this.ShowError(exception);
            }
        }

        private void btnGetPtzUrl_Click(object sender, EventArgs e)
        {
            try
            {
                this.Controller.GetAddress(new CapabilityCategory[1]);
            }
            catch (Exception exception)
            {
                this.ShowError(exception);
            }
        }

        private void btnStartAll_Click(object sender, EventArgs e)
        {
            this.ContinuousMove(true, true);
        }

        private void btnStartZoom_Click(object sender, EventArgs e)
        {
            this.ContinuousMove(false, true);
        }

        private void btnStopAll_Click(object sender, EventArgs e)
        {
            this.Stop(true, true);
        }

        private void btnStopZoom_Click(object sender, EventArgs e)
        {
            this.Stop(false, true);
        }

        private void btnVideo_Click(object sender, EventArgs e)
        {
            try
            {
                if (this._videoWindow == null)
                {
                    this.Controller.GetStreamUri();
                }
                else
                {
                    this._videoWindow.CloseWindow();
                    this._videoWindow = null;
                    this.btnVideo.Text = "Play Video";
                }
            }
            catch (Exception exception)
            {
                this.ShowError(exception);
            }
        }

        private void btnVxStartMove_Click(object sender, EventArgs e)
        {
            this.ContinuousMove(true, false);
        }

        private void btnVxStopMove_Click(object sender, EventArgs e)
        {
            this.Stop(true, false);
        }

        private void btnXFromMinToMax_Click(object sender, EventArgs e)
        {
            this.IncrementalAbsoluteRelativeMove(true, false, false);
            this.nudX.Value = 1M;
        }

        private void btnXMax_Click(object sender, EventArgs e)
        {
            if (this.nudX.Value == 1M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudX.Value = 1M;
            }
        }

        private void btnXMin_Click(object sender, EventArgs e)
        {
            if (this.nudX.Value == -1M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudX.Value = -1M;
            }
        }

        private void btnYFromMinToMax_Click(object sender, EventArgs e)
        {
            this.IncrementalAbsoluteRelativeMove(false, true, false);
            this.nudY.Value = 1M;
        }

        private void btnYMax_Click(object sender, EventArgs e)
        {
            if (this.nudY.Value == 1M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudY.Value = 1M;
            }
        }

        private void btnYMin_Click(object sender, EventArgs e)
        {
            if (this.nudY.Value == -1M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudY.Value = -1M;
            }
        }

        private void btnZoomFromMinToMax_Click(object sender, EventArgs e)
        {
            this.IncrementalAbsoluteRelativeMove(false, false, true);
            this.nudZoom.Value = 1M;
        }

        private void btnZoomMax_Click(object sender, EventArgs e)
        {
            if (this.nudZoom.Value == 1M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudZoom.Value = 1M;
            }
        }

        private void btnZoomMin_Click(object sender, EventArgs e)
        {
            if (this.nudZoom.Value == 0M)
            {
                this.AbsoluteRelativeMove();
            }
            else
            {
                this.nudZoom.Value = 0M;
            }
        }

        private void cmbPTZProfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
            PTZConfiguration configuration = (profile != null) ? profile.PTZConfiguration : null;
            this.btnAddPTZConfig.Enabled = configuration == null;
            this.panelAbsoluteMove.Enabled = ((this.rbAbsoluteMove.Checked || this.rbRelativeMove.Checked) && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
            this.panelContiniusMove.Enabled = (this.rbContuniousMove.Checked && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
            this.rbAbsoluteMove.Enabled = !string.IsNullOrEmpty(this.PTZAddress) && (configuration != null);
            this.rbRelativeMove.Enabled = !string.IsNullOrEmpty(this.PTZAddress) && (configuration != null);
            this.rbContuniousMove.Enabled = !string.IsNullOrEmpty(this.PTZAddress) && (configuration != null);
        }

        private void ContinuousMove(bool panTilt, bool zoom)
        {
            TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
            if (profile == null)
            {
                base.ShowPrompt("Select profile with PTZ configuration", "Error");
            }
            else
            {
                int result = -1;
                if (this.chkUseTimeout.Checked && (!int.TryParse(this.tbTimeout.Text, out result) || (result < 0)))
                {
                    base.ShowPrompt("Incorrect timeout", "Error");
                }
                else
                {
                    try
                    {
                        this.Controller.ContinuousMove(profile.token, panTilt, zoom, this.nudVx.Value, this.nudVy.Value, this.nudVzoom.Value, result);
                    }
                    catch (Exception exception)
                    {
                        this.ShowError(exception);
                    }
                }
            }
        }

        public void DisplayLog(string logEntry)
        {
            base.BeginInvoke(new Action(() => this.tbReport.Text = logEntry));
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void EnableControls(bool enable)
        {
            base.Invoke((Action)delegate
            {
                DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
                string str = (discoveredDevices != null) ? discoveredDevices.ServiceAddress : string.Empty;
                TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
                PTZConfiguration configuration = (profile != null) ? profile.PTZConfiguration : null;
                this.btnGetPtzUrl.Enabled = enable && !string.IsNullOrEmpty(str);
                this.btnGetProfiles.Enabled = enable && !string.IsNullOrEmpty(this.MediaAddress);
                this.btnVideo.Enabled = (enable && !string.IsNullOrEmpty(this.MediaAddress)) || (this._videoWindow != null);
                this.rbAbsoluteMove.Enabled = (enable && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
                this.rbRelativeMove.Enabled = (enable && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
                this.rbContuniousMove.Enabled = (enable && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
                this.panelAbsoluteMove.Enabled = ((enable && (this.rbAbsoluteMove.Checked || this.rbRelativeMove.Checked)) && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
                this.panelContiniusMove.Enabled = ((enable && this.rbContuniousMove.Checked) && !string.IsNullOrEmpty(this.PTZAddress)) && (configuration != null);
                this.btnAddPTZConfig.Enabled = (enable && (profile != null)) && (configuration == null);
                this.cmbPTZProfiles.Enabled = enable;
            });
        }

        public override IController GetController()
        {
            return this._controller;
        }

        private void IncrementalAbsoluteRelativeMove(bool x, bool y, bool z)
        {
            TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
            if (profile == null)
            {
                base.ShowPrompt("Select profile with PTZ configuration", "Error");
            }
            else
            {
                try
                {
                    this.Controller.AbosuteRelativeIncrementalMove(this.rbAbsoluteMove.Checked, profile.token, x ? -1M : this.nudX.Value, x ? 1M : this.nudX.Value, y ? -1M : this.nudY.Value, y ? 1M : this.nudY.Value, z ? 0M : this.nudZoom.Value, z ? 1M : this.nudZoom.Value);
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        private void InitializeComponent()
        {
            this.btnVideo = new Button();
            this.btnStopAll = new Button();
            this.btnStartAll = new Button();
            this.btnStopZoom = new Button();
            this.btnStartZoom = new Button();
            this.lblVZoom = new Label();
            this.nudVzoom = new NumericUpDown();
            this.lblVy = new Label();
            this.nudVy = new NumericUpDown();
            this.btnVxStopMove = new Button();
            this.btnVxStartMove = new Button();
            this.lblVx = new Label();
            this.nudVx = new NumericUpDown();
            this.rbAbsoluteMove = new RadioButton();
            this.btnZoomFromMinToMax = new Button();
            this.btnZoomMax = new Button();
            this.btnZoomMin = new Button();
            this.lblZoom = new Label();
            this.nudZoom = new NumericUpDown();
            this.btnYFromMinToMax = new Button();
            this.btnYMax = new Button();
            this.btnYMin = new Button();
            this.lblY = new Label();
            this.nudY = new NumericUpDown();
            this.btnXFromMinToMax = new Button();
            this.btnXMax = new Button();
            this.btnXMin = new Button();
            this.lblX = new Label();
            this.nudX = new NumericUpDown();
            this.rbContuniousMove = new RadioButton();
            this.btnGetPtzUrl = new Button();
            this.tbPtzUrl = new TextBox();
            this.lblPtzUrl = new Label();
            this.tbReport = new TextBox();
            this.panelAbsoluteMove = new Panel();
            this.panelContiniusMove = new Panel();
            this.tbTimeout = new TextBox();
            this.chkUseTimeout = new CheckBox();
            this.label1 = new Label();
            this.cmbPTZProfiles = new ComboBox();
            this.btnGetProfiles = new Button();
            this.label2 = new Label();
            this.tbMediaUrl = new TextBox();
            this.btnAddPTZConfig = new Button();
            this.rbRelativeMove = new RadioButton();
            this.gbProfile = new GroupBox();
            this.gbUrls = new Panel();
            this.groupBox1 = new GroupBox();
            this.nudVzoom.BeginInit();
            this.nudVy.BeginInit();
            this.nudVx.BeginInit();
            this.nudZoom.BeginInit();
            this.nudY.BeginInit();
            this.nudX.BeginInit();
            this.panelAbsoluteMove.SuspendLayout();
            this.panelContiniusMove.SuspendLayout();
            this.gbProfile.SuspendLayout();
            this.gbUrls.SuspendLayout();
            this.groupBox1.SuspendLayout();
            base.SuspendLayout();
            this.btnVideo.Location = new Point(6, 0x1a8);
            this.btnVideo.Name = "btnVideo";
            this.btnVideo.Size = new Size(0x4b, 0x17);
            this.btnVideo.TabIndex = 0x21;
            this.btnVideo.Text = "Play Video";
            this.btnVideo.UseVisualStyleBackColor = true;
            this.btnVideo.Click += new EventHandler(this.btnVideo_Click);
            this.btnStopAll.Location = new Point(200, 0x5b);
            this.btnStopAll.Name = "btnStopAll";
            this.btnStopAll.Size = new Size(0x44, 0x17);
            this.btnStopAll.TabIndex = 30;
            this.btnStopAll.Text = "Stop All";
            this.btnStopAll.UseVisualStyleBackColor = true;
            this.btnStopAll.Click += new EventHandler(this.btnStopAll_Click);
            this.btnStartAll.Location = new Point(0x7e, 0x5b);
            this.btnStartAll.Name = "btnStartAll";
            this.btnStartAll.Size = new Size(0x44, 0x17);
            this.btnStartAll.TabIndex = 0x1d;
            this.btnStartAll.Text = "Start All";
            this.btnStartAll.UseVisualStyleBackColor = true;
            this.btnStartAll.Click += new EventHandler(this.btnStartAll_Click);
            this.btnStopZoom.Location = new Point(200, 0x3e);
            this.btnStopZoom.Name = "btnStopZoom";
            this.btnStopZoom.Size = new Size(0x44, 0x17);
            this.btnStopZoom.TabIndex = 0x1c;
            this.btnStopZoom.Text = "Stop Zoom";
            this.btnStopZoom.UseVisualStyleBackColor = true;
            this.btnStopZoom.Click += new EventHandler(this.btnStopZoom_Click);
            this.btnStartZoom.Location = new Point(0x7e, 0x3e);
            this.btnStartZoom.Name = "btnStartZoom";
            this.btnStartZoom.Size = new Size(0x44, 0x17);
            this.btnStartZoom.TabIndex = 0x1b;
            this.btnStartZoom.Text = "Start Zoom";
            this.btnStartZoom.UseVisualStyleBackColor = true;
            this.btnStartZoom.Click += new EventHandler(this.btnStartZoom_Click);
            this.lblVZoom.AutoSize = true;
            this.lblVZoom.Location = new Point(11, 0x43);
            this.lblVZoom.Name = "lblVZoom";
            this.lblVZoom.Size = new Size(0x2c, 13);
            this.lblVZoom.TabIndex = 0x4b;
            this.lblVZoom.Text = "VZoom:";
            this.nudVzoom.DecimalPlaces = 2;
            int[] bits = new int[4];
            bits[0] = 1;
            bits[3] = 0x10000;
            this.nudVzoom.Increment = new decimal(bits);
            this.nudVzoom.Location = new Point(0x38, 0x41);
            int[] numArray2 = new int[4];
            numArray2[0] = 1;
            this.nudVzoom.Maximum = new decimal(numArray2);
            int[] numArray3 = new int[4];
            numArray3[0] = 1;
            numArray3[3] = -2147483648;
            this.nudVzoom.Minimum = new decimal(numArray3);
            this.nudVzoom.Name = "nudVzoom";
            this.nudVzoom.Size = new Size(0x40, 20);
            this.nudVzoom.TabIndex = 0x1a;
            this.lblVy.AutoSize = true;
            this.lblVy.Location = new Point(11, 0x26);
            this.lblVy.Name = "lblVy";
            this.lblVy.Size = new Size(0x18, 13);
            this.lblVy.TabIndex = 0x49;
            this.lblVy.Text = "VY:";
            this.nudVy.DecimalPlaces = 2;
            int[] numArray4 = new int[4];
            numArray4[0] = 1;
            numArray4[3] = 0x10000;
            this.nudVy.Increment = new decimal(numArray4);
            this.nudVy.Location = new Point(0x38, 0x24);
            int[] numArray5 = new int[4];
            numArray5[0] = 1;
            this.nudVy.Maximum = new decimal(numArray5);
            int[] numArray6 = new int[4];
            numArray6[0] = 1;
            numArray6[3] = -2147483648;
            this.nudVy.Minimum = new decimal(numArray6);
            this.nudVy.Name = "nudVy";
            this.nudVy.Size = new Size(0x40, 20);
            this.nudVy.TabIndex = 0x19;
            this.btnVxStopMove.Location = new Point(200, 5);
            this.btnVxStopMove.Name = "btnVxStopMove";
            this.btnVxStopMove.Size = new Size(0x44, 0x17);
            this.btnVxStopMove.TabIndex = 0x18;
            this.btnVxStopMove.Text = "Stop Move";
            this.btnVxStopMove.UseVisualStyleBackColor = true;
            this.btnVxStopMove.Click += new EventHandler(this.btnVxStopMove_Click);
            this.btnVxStartMove.Location = new Point(0x7e, 5);
            this.btnVxStartMove.Name = "btnVxStartMove";
            this.btnVxStartMove.Size = new Size(0x44, 0x17);
            this.btnVxStartMove.TabIndex = 0x17;
            this.btnVxStartMove.Text = "Start Move";
            this.btnVxStartMove.UseVisualStyleBackColor = true;
            this.btnVxStartMove.Click += new EventHandler(this.btnVxStartMove_Click);
            this.lblVx.AutoSize = true;
            this.lblVx.Location = new Point(10, 9);
            this.lblVx.Name = "lblVx";
            this.lblVx.Size = new Size(0x18, 13);
            this.lblVx.TabIndex = 0x45;
            this.lblVx.Text = "VX:";
            this.nudVx.DecimalPlaces = 2;
            int[] numArray7 = new int[4];
            numArray7[0] = 1;
            numArray7[3] = 0x10000;
            this.nudVx.Increment = new decimal(numArray7);
            this.nudVx.Location = new Point(0x38, 7);
            int[] numArray8 = new int[4];
            numArray8[0] = 1;
            this.nudVx.Maximum = new decimal(numArray8);
            int[] numArray9 = new int[4];
            numArray9[0] = 1;
            numArray9[3] = -2147483648;
            this.nudVx.Minimum = new decimal(numArray9);
            this.nudVx.Name = "nudVx";
            this.nudVx.Size = new Size(0x40, 20);
            this.nudVx.TabIndex = 0x16;
            this.rbAbsoluteMove.AutoSize = true;
            this.rbAbsoluteMove.Checked = true;
            this.rbAbsoluteMove.Location = new Point(6, 0x13);
            this.rbAbsoluteMove.Name = "rbAbsoluteMove";
            this.rbAbsoluteMove.Size = new Size(0x60, 0x11);
            this.rbAbsoluteMove.TabIndex = 3;
            this.rbAbsoluteMove.TabStop = true;
            this.rbAbsoluteMove.Text = "Absolute Move";
            this.rbAbsoluteMove.UseVisualStyleBackColor = true;
            this.rbAbsoluteMove.CheckedChanged += new EventHandler(this.rbMode_CheckedChanged);
            this.btnZoomFromMinToMax.Location = new Point(0xe3, 60);
            this.btnZoomFromMinToMax.Name = "btnZoomFromMinToMax";
            this.btnZoomFromMinToMax.Size = new Size(0x73, 0x17);
            this.btnZoomFromMinToMax.TabIndex = 0x10;
            this.btnZoomFromMinToMax.Text = "From Min To Max";
            this.btnZoomFromMinToMax.UseVisualStyleBackColor = true;
            this.btnZoomFromMinToMax.Click += new EventHandler(this.btnZoomFromMinToMax_Click);
            this.btnZoomMax.Location = new Point(0xb0, 0x3d);
            this.btnZoomMax.Name = "btnZoomMax";
            this.btnZoomMax.Size = new Size(0x2d, 0x17);
            this.btnZoomMax.TabIndex = 15;
            this.btnZoomMax.Text = "Max";
            this.btnZoomMax.UseVisualStyleBackColor = true;
            this.btnZoomMax.Click += new EventHandler(this.btnZoomMax_Click);
            this.btnZoomMin.Location = new Point(0x7d, 0x3d);
            this.btnZoomMin.Name = "btnZoomMin";
            this.btnZoomMin.Size = new Size(0x2d, 0x17);
            this.btnZoomMin.TabIndex = 14;
            this.btnZoomMin.Text = "Min";
            this.btnZoomMin.UseVisualStyleBackColor = true;
            this.btnZoomMin.Click += new EventHandler(this.btnZoomMin_Click);
            this.lblZoom.AutoSize = true;
            this.lblZoom.Location = new Point(10, 0x41);
            this.lblZoom.Name = "lblZoom";
            this.lblZoom.Size = new Size(0x25, 13);
            this.lblZoom.TabIndex = 0x3f;
            this.lblZoom.Text = "Zoom:";
            this.nudZoom.DecimalPlaces = 2;
            int[] numArray10 = new int[4];
            numArray10[0] = 1;
            numArray10[3] = 0x10000;
            this.nudZoom.Increment = new decimal(numArray10);
            this.nudZoom.Location = new Point(0x37, 0x3f);
            int[] numArray11 = new int[4];
            numArray11[0] = 1;
            this.nudZoom.Maximum = new decimal(numArray11);
            int[] numArray12 = new int[4];
            numArray12[0] = 1;
            numArray12[3] = -2147483648;
            this.nudZoom.Minimum = new decimal(numArray12);
            this.nudZoom.Name = "nudZoom";
            this.nudZoom.Size = new Size(0x40, 20);
            this.nudZoom.TabIndex = 13;
            this.nudZoom.ValueChanged += new EventHandler(this.nudZoom_ValueChanged);
            this.btnYFromMinToMax.Location = new Point(0xe3, 0x1f);
            this.btnYFromMinToMax.Name = "btnYFromMinToMax";
            this.btnYFromMinToMax.Size = new Size(0x73, 0x17);
            this.btnYFromMinToMax.TabIndex = 12;
            this.btnYFromMinToMax.Text = "From Min To Max";
            this.btnYFromMinToMax.UseVisualStyleBackColor = true;
            this.btnYFromMinToMax.Click += new EventHandler(this.btnYFromMinToMax_Click);
            this.btnYMax.Location = new Point(0xb0, 0x1f);
            this.btnYMax.Name = "btnYMax";
            this.btnYMax.Size = new Size(0x2d, 0x17);
            this.btnYMax.TabIndex = 11;
            this.btnYMax.Text = "Max";
            this.btnYMax.UseVisualStyleBackColor = true;
            this.btnYMax.Click += new EventHandler(this.btnYMax_Click);
            this.btnYMin.Location = new Point(0x7d, 0x1f);
            this.btnYMin.Name = "btnYMin";
            this.btnYMin.Size = new Size(0x2d, 0x17);
            this.btnYMin.TabIndex = 10;
            this.btnYMin.Text = "Min";
            this.btnYMin.UseVisualStyleBackColor = true;
            this.btnYMin.Click += new EventHandler(this.btnYMin_Click);
            this.lblY.AutoSize = true;
            this.lblY.Location = new Point(10, 0x24);
            this.lblY.Name = "lblY";
            this.lblY.Size = new Size(0x11, 13);
            this.lblY.TabIndex = 0x3a;
            this.lblY.Text = "Y:";
            this.nudY.DecimalPlaces = 2;
            int[] numArray13 = new int[4];
            numArray13[0] = 1;
            numArray13[3] = 0x10000;
            this.nudY.Increment = new decimal(numArray13);
            this.nudY.Location = new Point(0x37, 0x22);
            int[] numArray14 = new int[4];
            numArray14[0] = 1;
            this.nudY.Maximum = new decimal(numArray14);
            int[] numArray15 = new int[4];
            numArray15[0] = 1;
            numArray15[3] = -2147483648;
            this.nudY.Minimum = new decimal(numArray15);
            this.nudY.Name = "nudY";
            this.nudY.Size = new Size(0x40, 20);
            this.nudY.TabIndex = 9;
            this.nudY.ValueChanged += new EventHandler(this.nudY_ValueChanged);
            this.btnXFromMinToMax.Location = new Point(0xe3, 3);
            this.btnXFromMinToMax.Name = "btnXFromMinToMax";
            this.btnXFromMinToMax.Size = new Size(0x73, 0x17);
            this.btnXFromMinToMax.TabIndex = 8;
            this.btnXFromMinToMax.Text = "From Min To Max";
            this.btnXFromMinToMax.UseVisualStyleBackColor = true;
            this.btnXFromMinToMax.Click += new EventHandler(this.btnXFromMinToMax_Click);
            this.btnXMax.Location = new Point(0xb0, 3);
            this.btnXMax.Name = "btnXMax";
            this.btnXMax.Size = new Size(0x2d, 0x17);
            this.btnXMax.TabIndex = 7;
            this.btnXMax.Text = "Max";
            this.btnXMax.UseVisualStyleBackColor = true;
            this.btnXMax.Click += new EventHandler(this.btnXMax_Click);
            this.btnXMin.Location = new Point(0x7d, 3);
            this.btnXMin.Name = "btnXMin";
            this.btnXMin.Size = new Size(0x2d, 0x17);
            this.btnXMin.TabIndex = 6;
            this.btnXMin.Text = "Min";
            this.btnXMin.UseVisualStyleBackColor = true;
            this.btnXMin.Click += new EventHandler(this.btnXMin_Click);
            this.lblX.AutoSize = true;
            this.lblX.Location = new Point(9, 7);
            this.lblX.Name = "lblX";
            this.lblX.Size = new Size(0x11, 13);
            this.lblX.TabIndex = 0x35;
            this.lblX.Text = "X:";
            this.nudX.DecimalPlaces = 2;
            int[] numArray16 = new int[4];
            numArray16[0] = 1;
            numArray16[3] = 0x10000;
            this.nudX.Increment = new decimal(numArray16);
            this.nudX.Location = new Point(0x37, 5);
            int[] numArray17 = new int[4];
            numArray17[0] = 1;
            this.nudX.Maximum = new decimal(numArray17);
            int[] numArray18 = new int[4];
            numArray18[0] = 1;
            numArray18[3] = -2147483648;
            this.nudX.Minimum = new decimal(numArray18);
            this.nudX.Name = "nudX";
            this.nudX.Size = new Size(0x40, 20);
            this.nudX.TabIndex = 5;
            this.nudX.ValueChanged += new EventHandler(this.nudX_ValueChanged);
            this.rbContuniousMove.AutoSize = true;
            this.rbContuniousMove.Location = new Point(6, 0x81);
            this.rbContuniousMove.Name = "rbContuniousMove";
            this.rbContuniousMove.Size = new Size(0x6c, 0x11);
            this.rbContuniousMove.TabIndex = 20;
            this.rbContuniousMove.TabStop = true;
            this.rbContuniousMove.Text = "Continuous Move";
            this.rbContuniousMove.UseVisualStyleBackColor = true;
            this.rbContuniousMove.CheckedChanged += new EventHandler(this.rbMode_CheckedChanged);
            this.btnGetPtzUrl.Location = new Point(0x139, 4);
            this.btnGetPtzUrl.Name = "btnGetPtzUrl";
            this.btnGetPtzUrl.Size = new Size(0x34, 0x2f);
            this.btnGetPtzUrl.TabIndex = 2;
            this.btnGetPtzUrl.Text = "Get URLs";
            this.btnGetPtzUrl.UseVisualStyleBackColor = true;
            this.btnGetPtzUrl.Click += new EventHandler(this.btnGetPtzUrl_Click);
            this.tbPtzUrl.Location = new Point(0x45, 0x1f);
            this.tbPtzUrl.Name = "tbPtzUrl";
            this.tbPtzUrl.ReadOnly = true;
            this.tbPtzUrl.Size = new Size(0xee, 20);
            this.tbPtzUrl.TabIndex = 1;
            this.lblPtzUrl.Location = new Point(4, 7);
            this.lblPtzUrl.Name = "lblPtzUrl";
            this.lblPtzUrl.Size = new Size(0x40, 13);
            this.lblPtzUrl.TabIndex = 0x30;
            this.lblPtzUrl.Text = "Media URL:";
            this.tbReport.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.tbReport.Location = new Point(0x173, 4);
            this.tbReport.Multiline = true;
            this.tbReport.Name = "tbReport";
            this.tbReport.ReadOnly = true;
            this.tbReport.ScrollBars = ScrollBars.Vertical;
            this.tbReport.Size = new Size(0x18c, 450);
            this.tbReport.TabIndex = 0x22;
            this.panelAbsoluteMove.Controls.Add(this.lblX);
            this.panelAbsoluteMove.Controls.Add(this.nudX);
            this.panelAbsoluteMove.Controls.Add(this.btnXMin);
            this.panelAbsoluteMove.Controls.Add(this.btnXMax);
            this.panelAbsoluteMove.Controls.Add(this.btnXFromMinToMax);
            this.panelAbsoluteMove.Controls.Add(this.nudY);
            this.panelAbsoluteMove.Controls.Add(this.lblY);
            this.panelAbsoluteMove.Controls.Add(this.btnYMin);
            this.panelAbsoluteMove.Controls.Add(this.btnYMax);
            this.panelAbsoluteMove.Controls.Add(this.btnYFromMinToMax);
            this.panelAbsoluteMove.Controls.Add(this.nudZoom);
            this.panelAbsoluteMove.Controls.Add(this.lblZoom);
            this.panelAbsoluteMove.Controls.Add(this.btnZoomMin);
            this.panelAbsoluteMove.Controls.Add(this.btnZoomMax);
            this.panelAbsoluteMove.Controls.Add(this.btnZoomFromMinToMax);
            this.panelAbsoluteMove.Location = new Point(11, 0x24);
            this.panelAbsoluteMove.Name = "panelAbsoluteMove";
            this.panelAbsoluteMove.Size = new Size(0x158, 0x59);
            this.panelAbsoluteMove.TabIndex = 4;
            this.panelContiniusMove.Controls.Add(this.tbTimeout);
            this.panelContiniusMove.Controls.Add(this.chkUseTimeout);
            this.panelContiniusMove.Controls.Add(this.lblVx);
            this.panelContiniusMove.Controls.Add(this.nudVx);
            this.panelContiniusMove.Controls.Add(this.btnVxStartMove);
            this.panelContiniusMove.Controls.Add(this.btnVxStopMove);
            this.panelContiniusMove.Controls.Add(this.nudVy);
            this.panelContiniusMove.Controls.Add(this.btnStopAll);
            this.panelContiniusMove.Controls.Add(this.lblVy);
            this.panelContiniusMove.Controls.Add(this.btnStartAll);
            this.panelContiniusMove.Controls.Add(this.nudVzoom);
            this.panelContiniusMove.Controls.Add(this.btnStopZoom);
            this.panelContiniusMove.Controls.Add(this.lblVZoom);
            this.panelContiniusMove.Controls.Add(this.btnStartZoom);
            this.panelContiniusMove.Enabled = false;
            this.panelContiniusMove.Location = new Point(11, 0x98);
            this.panelContiniusMove.Name = "panelContiniusMove";
            this.panelContiniusMove.Size = new Size(0x158, 150);
            this.panelContiniusMove.TabIndex = 0x15;
            this.tbTimeout.Location = new Point(0x7d, 120);
            this.tbTimeout.Name = "tbTimeout";
            this.tbTimeout.Size = new Size(0x31, 20);
            this.tbTimeout.TabIndex = 0x20;
            this.tbTimeout.Text = "10";
            this.chkUseTimeout.AutoSize = true;
            this.chkUseTimeout.Location = new Point(11, 0x7b);
            this.chkUseTimeout.Name = "chkUseTimeout";
            this.chkUseTimeout.Size = new Size(0x6c, 0x11);
            this.chkUseTimeout.TabIndex = 0x1f;
            this.chkUseTimeout.Text = "Use Timeout (ms)";
            this.chkUseTimeout.UseVisualStyleBackColor = true;
            this.label1.AutoSize = true;
            this.label1.Location = new Point(3, 0x13);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x27, 13);
            this.label1.TabIndex = 0x31;
            this.label1.Text = "Profile:";
            this.cmbPTZProfiles.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cmbPTZProfiles.FormattingEnabled = true;
            this.cmbPTZProfiles.Location = new Point(50, 0x10);
            this.cmbPTZProfiles.Name = "cmbPTZProfiles";
            this.cmbPTZProfiles.Size = new Size(0x83, 0x15);
            this.cmbPTZProfiles.TabIndex = 50;
            this.cmbPTZProfiles.SelectedIndexChanged += new EventHandler(this.cmbPTZProfiles_SelectedIndexChanged);
            this.btnGetProfiles.Location = new Point(0xbb, 14);
            this.btnGetProfiles.Name = "btnGetProfiles";
            this.btnGetProfiles.Size = new Size(0x2d, 0x17);
            this.btnGetProfiles.TabIndex = 0x33;
            this.btnGetProfiles.Text = "Get";
            this.btnGetProfiles.UseVisualStyleBackColor = true;
            this.btnGetProfiles.Click += new EventHandler(this.btnGetProfiles_Click);
            this.label2.AutoSize = true;
            this.label2.Location = new Point(4, 0x22);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x38, 13);
            this.label2.TabIndex = 0x34;
            this.label2.Text = "PTZ URL:";
            this.tbMediaUrl.Location = new Point(0x45, 4);
            this.tbMediaUrl.Name = "tbMediaUrl";
            this.tbMediaUrl.ReadOnly = true;
            this.tbMediaUrl.Size = new Size(0xee, 20);
            this.tbMediaUrl.TabIndex = 0x35;
            this.btnAddPTZConfig.Location = new Point(0xee, 14);
            this.btnAddPTZConfig.Name = "btnAddPTZConfig";
            this.btnAddPTZConfig.Size = new Size(0x70, 0x17);
            this.btnAddPTZConfig.TabIndex = 0x36;
            this.btnAddPTZConfig.Text = "Add PTZ config";
            this.btnAddPTZConfig.UseVisualStyleBackColor = true;
            this.btnAddPTZConfig.Click += new EventHandler(this.btnAddPTZConfig_Click);
            this.rbRelativeMove.AutoSize = true;
            this.rbRelativeMove.Location = new Point(0x6c, 0x13);
            this.rbRelativeMove.Name = "rbRelativeMove";
            this.rbRelativeMove.Size = new Size(0x5e, 0x11);
            this.rbRelativeMove.TabIndex = 4;
            this.rbRelativeMove.Text = "Relative Move";
            this.rbRelativeMove.UseVisualStyleBackColor = true;
            this.rbRelativeMove.CheckedChanged += new EventHandler(this.rbMode_CheckedChanged);
            this.gbProfile.Controls.Add(this.btnAddPTZConfig);
            this.gbProfile.Controls.Add(this.label1);
            this.gbProfile.Controls.Add(this.cmbPTZProfiles);
            this.gbProfile.Controls.Add(this.btnGetProfiles);
            this.gbProfile.Location = new Point(6, 0x40);
            this.gbProfile.Name = "gbProfile";
            this.gbProfile.Size = new Size(0x169, 0x2b);
            this.gbProfile.TabIndex = 0x37;
            this.gbProfile.TabStop = false;
            this.gbProfile.Text = "Media Profile";
            this.gbUrls.Controls.Add(this.tbPtzUrl);
            this.gbUrls.Controls.Add(this.label2);
            this.gbUrls.Controls.Add(this.tbMediaUrl);
            this.gbUrls.Controls.Add(this.btnGetPtzUrl);
            this.gbUrls.Controls.Add(this.lblPtzUrl);
            this.gbUrls.Location = new Point(0, 0);
            this.gbUrls.Name = "gbUrls";
            this.gbUrls.Size = new Size(0x16d, 0x3a);
            this.gbUrls.TabIndex = 0x38;
            this.gbUrls.Text = "Services";
            this.groupBox1.Controls.Add(this.rbAbsoluteMove);
            this.groupBox1.Controls.Add(this.rbContuniousMove);
            this.groupBox1.Controls.Add(this.panelAbsoluteMove);
            this.groupBox1.Controls.Add(this.rbRelativeMove);
            this.groupBox1.Controls.Add(this.panelContiniusMove);
            this.groupBox1.Location = new Point(6, 0x71);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0x169, 0x131);
            this.groupBox1.TabIndex = 0x39;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "PTZ Control";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.groupBox1);
            base.Controls.Add(this.gbUrls);
            base.Controls.Add(this.gbProfile);
            base.Controls.Add(this.btnVideo);
            base.Controls.Add(this.tbReport);
            base.Name = "PtzPage";
            base.Size = new Size(0x307, 460);
            this.nudVzoom.EndInit();
            this.nudVy.EndInit();
            this.nudVx.EndInit();
            this.nudZoom.EndInit();
            this.nudY.EndInit();
            this.nudX.EndInit();
            this.panelAbsoluteMove.ResumeLayout(false);
            this.panelAbsoluteMove.PerformLayout();
            this.panelContiniusMove.ResumeLayout(false);
            this.panelContiniusMove.PerformLayout();
            this.gbProfile.ResumeLayout(false);
            this.gbProfile.PerformLayout();
            this.gbUrls.ResumeLayout(false);
            this.gbUrls.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void nudX_ValueChanged(object sender, EventArgs e)
        {
            this.AbsoluteRelativeMove();
        }

        private void nudY_ValueChanged(object sender, EventArgs e)
        {
            this.AbsoluteRelativeMove();
        }

        private void nudZoom_ValueChanged(object sender, EventArgs e)
        {
            this.AbsoluteRelativeMove();
        }

        public void OnPTZConfigurationAdded(string profile, string config)
        {
            base.Invoke((Action)delegate
            {
                foreach (object obj2 in this.cmbPTZProfiles.Items)
                {
                    TestTool.Proxies.Onvif.Profile profile1 = (obj2 as ProfileWrapper).Profile;
                    if (profile1.token == profile)
                    {
                        profile1.PTZConfiguration = new PTZConfiguration();
                        break;
                    }
                }
            });
        }

        private void rbMode_CheckedChanged(object sender, EventArgs e)
        {
            this.panelAbsoluteMove.Enabled = this.rbAbsoluteMove.Checked || this.rbRelativeMove.Checked;
            this.panelContiniusMove.Enabled = this.rbContuniousMove.Checked;
        }

        public void SetProfiles(TestTool.Proxies.Onvif.Profile[] profiles)
        {
            base.Invoke((Action)delegate
            {
                this.cmbPTZProfiles.Items.Clear();
                foreach (TestTool.Proxies.Onvif.Profile profile in profiles)
                {
                    ProfileWrapper item = new ProfileWrapper
                    {
                        Profile = profile
                    };
                    this.cmbPTZProfiles.Items.Add(item);
                }
                if (this.cmbPTZProfiles.Items.Count > 0)
                {
                    this.cmbPTZProfiles.SelectedIndex = 0;
                }
            });
        }

        public void ShowVideo(MediaUri uri, VideoEncoderConfiguration encoder)
        {
            Action method = null;
            try
            {
                this._videoWindow = new VideoContainer();
                DeviceEnvironment deviceEnvironment = ContextController.GetDeviceEnvironment();
                int message = deviceEnvironment.Timeouts.Message;
                VideoUtils.AdjustVideo(this._videoWindow, deviceEnvironment.Credentials.UserName, deviceEnvironment.Credentials.Password, message, TransportProtocol.UDP, StreamType.RTPUnicast, uri, encoder);
                this._videoWindow.KEEPALIVE = true;
                this._videoWindow.OpenWindow(false);
                if (method == null)
                {
                    method = () => this.btnVideo.Text = "Stop Video";
                }
                base.Invoke(method);
            }
            catch
            {
                this._videoWindow = null;
                throw;
            }
        }

        private void Stop(bool panTilt, bool zoom)
        {
            TestTool.Proxies.Onvif.Profile profile = (this.cmbPTZProfiles.SelectedItem != null) ? (this.cmbPTZProfiles.SelectedItem as ProfileWrapper).Profile : null;
            if (profile == null)
            {
                base.ShowPrompt("Select profile with PTZ configuration", "Error");
            }
            else
            {
                try
                {
                    this.Controller.Stop(profile.token, panTilt, zoom);
                }
                catch (Exception exception)
                {
                    this.ShowError(exception);
                }
            }
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            if (state.IsActive())
            {
                this.EnableControls(false);
            }
            else
            {
                this.EnableControls(true);
            }
        }

        internal PtzController Controller
        {
            get
            {
                return this._controller;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string MediaAddress
        {
            get
            {
                return this.tbMediaUrl.Text;
            }
            set
            {
                base.BeginInvoke((Action)delegate
                {
                    this.tbMediaUrl.Text = value;
                    this.cmbPTZProfiles.Items.Clear();
                });
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PTZAddress
        {
            get
            {
                return this.tbPtzUrl.Text;
            }
            set
            {
                base.BeginInvoke(new Action(() => this.tbPtzUrl.Text = value));
            }
        }

        private class ProfileWrapper
        {
            public override string ToString()
            {
                if (this.Profile == null)
                {
                    return string.Empty;
                }
                if (string.IsNullOrEmpty(this.Profile.Name))
                {
                    return this.Profile.token;
                }
                return string.Format("{0} ({1})", this.Profile.Name, this.Profile.token);
            }

            public TestTool.Proxies.Onvif.Profile Profile { get; set; }
        }
    }
}

